package attori.controller;

import sde.actor.Actor;
import sde.actor.Message;
import attori.attuatori.AttuatoreTemperaturaIF;

public class ControllerTemperatura extends Actor implements
		ControllerTemperaturaIF {

	public static final byte CREATED = 0, OPERATIVE = 1;
	private int tMax;
	private int tMin;
	private AttuatoreTemperaturaIF termostato;

	public ControllerTemperatura() {
		become(CREATED);
	}

	@Override
	protected void handler(Message m) {
		switch (currentStatus()) {
		case CREATED:
			if (m instanceof Init) {
				tMax = ((Init) m).tMax;
				tMin = ((Init) m).tMin;
				termostato = ((Init) m).termostato;
				become(OPERATIVE);
			}
			break;
		case OPERATIVE:
			if (m instanceof Temperatura) {
				int temperatureCorrente = ((Temperatura) m).tCurr;
				if (temperatureCorrente > tMax) {
					termostato.send(new AttuatoreTemperaturaIF.Diminuisci(
							temperatureCorrente - tMax));
				} else if (temperatureCorrente < tMin) {
					termostato.send(new AttuatoreTemperaturaIF.Aumenta(tMin
							- temperatureCorrente));
				} else {
					termostato.send(new AttuatoreTemperaturaIF.ToIdle());
				}
			} else if (m instanceof setTemperatura) {
				tMax = ((setTemperatura) m).tMax;
				tMin = ((setTemperatura) m).tMin;
				become(OPERATIVE);

			}
			break;
		}
	}
}
